/* --- Swazz Javascript Calendar ---
/* --- v 1.0 3rd November 2006
By Oliver Bryant
http://calendar.swazz.org */

function getObj(objID)
{
    if (document.getElementById) {return document.getElementById(objID);}
    else if (document.all) {return document.all[objID];}
    else if (document.layers) {return document.layers[objID];}
}

function checkClick(e) {
	e?evt=e:evt=event;
	CSE=evt.target?evt.target:evt.srcElement;
	if (getObj('fc'))
		if (!isChild(CSE,getObj('fc')))
			getObj('fc').style.display='none';
}

function isChild(s,d) {
	while(s) {
		if (s==d) 
			return true;
		s=s.parentNode;
	}
	return false;
}

function Left(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function Top(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
	
document.write('<table id="fc" style="position:absolute;border-collapse:collapse;background:#FFFFFF;border:1px solid #ABABAB;display:none" cellpadding=2>');
document.write('<tr><td style="cursor:pointer" onclick="csubm()"><img src="Images/arrowleftmonth.gif"></td><td colspan=5 id="mns" align="center" style="font:bold 13px Arial"></td><td align="right" style="cursor:pointer" onclick="caddm()"><img src="Images/arrowrightmonth.gif"></td></tr>');
document.write('<tr><td align=center style="background:#BEBEBE;font:12px Arial">S</td><td align=center style="background:#BEBEBE;font:12px Arial">M</td><td align=center style="background:#BEBEBE;font:12px Arial">T</td><td align=center style="background:#BEBEBE;font:12px Arial">W</td><td align=center style="background:#BEBEBE;font:12px Arial">T</td><td align=center style="background:#BEBEBE;font:12px Arial">F</td><td align=center style="background:#BEBEBE;font:12px Arial">S</td></tr>');
for(var kk=1;kk<=6;kk++) {
	document.write('<tr>');
	for(var tt=1;tt<=7;tt++) {
		num=7 * (kk-1) - (-tt);
		document.write('<td id="v' + num + '" style="width:18px;height:18px">&nbsp;</td>');
	}
	document.write('</tr>');
}
document.write('</table>');

document.all?document.attachEvent('onclick',checkClick):document.addEventListener('click',checkClick,false);


// Calendar script
var now = new Date;
var sccm=now.getMonth();
var sccy=now.getFullYear();
var ccm=now.getMonth();
var ccy=now.getFullYear();

var updobj = null;
function lcs(ielem) {
	updobj=ielem;
	var posOffset = Position.cumulativeOffset(ielem);
	var inputHeight = ielem.getHeight();
	$('fc').style.left = posOffset[0] + "px";
	$('fc').style.top = (posOffset[1] + inputHeight) + "px";
	$('fc').style.zIndex = 1000;
	
	//getObj('fc').style.left=Left(ielem);
	//getObj('fc').style.top=Top(ielem)+ielem.offsetHeight;
	$('fc').style.display='';
	
	var noValidStartDate = true;  // wgo
	var noValidEndDate = true;     // wgo
	var validCurrDate = false;  // wgo
	var startDateField = ielem.previous(1);  // wgo
	var startDateFieldValue = (startDateField) ? startDateField.value : "";  //wgo
	var endDateField = ielem.next(1);  // wgo
	var endDateFieldValue = (endDateField) ? endDateField.value : "";  //wgo
	
	// First check date is valid
	curdt=ielem.value;
	curdtarr=curdt.split('/');
	isdt=true;
	for(var k=0;k<curdtarr.length;k++) {
		if (isNaN(curdtarr[k]))
			isdt=false;
	}
	if (isdt&(curdtarr.length==3)) { // use current date if valid
		ccm=curdtarr[0]-1;
		ccy=curdtarr[2];
		prepcalendar(curdtarr[1],curdtarr[0]-1,curdtarr[2]);
		validCurrDate = true;
	} else if ( (startDateFieldValue != '') && 
                ((startDateField.hasClassName('date')) || (startDateField.hasClassName('aspDateBox'))) ) { // if field is end date, attempt to use start date
	    //var startDateValue = startDateField.value;
	    // check if this value is a valid date
	    startDateArr=startDateFieldValue.split('/');
	    isdt=true;
	    for(var k=0;k<startDateFieldValue.length;k++) {
		    if (isNaN(startDateArr[k]))
			    isdt=false;
	    }
	    // use the start date value
	    if (isdt&(startDateArr.length==3)) {
		    ccm=startDateArr[0]-1;
		    ccy=startDateArr[2];
		    prepcalendar(startDateArr[1],startDateArr[0]-1,startDateArr[2]);
	    } else {
	        noValidStartDate = false;
	    }
	} else if ( (endDateFieldValue != '') && 
	            ((endDateField.hasClassName('date')) || (endDateField.hasClassName('aspDateBox'))) ) { // if field is start date, attempt to use end date
	    //var endDateValue = endDateField.value;
	    // check if this value is a valid date
	    endDateArr=endDateFieldValue.split('/');
	    isdt=true;
	    for(var k=0;k<endDateFieldValue.length;k++) {
		    if (isNaN(endDateArr[k]))
			    isdt=false;
	    }
	    // use the end date value
	    if (isdt&(endDateArr.length==3)) {
		    ccm=endDateArr[0]-1;
		    ccy=endDateArr[2];
		    prepcalendar(endDateArr[1],endDateArr[0]-1,endDateArr[2]);
	    } else {
	        noValidEndDate = false;
	    }
	} 

    // use current date if no other dates are valid
    if (noValidStartDate && noValidEndDate && !validCurrDate) {
        var now = new Date;
        ccm = now.getMonth();
        ccy=now.getFullYear();
        prepcalendar('',ccm,ccy);
    }
}

function evtTgt(e)
{
	var el;
	if(e.target)el=e.target;
	else if(e.srcElement)el=e.srcElement;
	if(el.nodeType==3)el=el.parentNode; // defeat Safari bug
	return el;
}
function EvtObj(e){if(!e)e=window.event;return e;}
function cs_over(e) {
	evtTgt(EvtObj(e)).style.background='#FFCC66';
}
function cs_out(e) {
	evtTgt(EvtObj(e)).style.background='#C4D3EA';
}
function cs_click(e) {
	if (!updobj.hasClassName('dataTagDate')) {
	    updobj.value=calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)];
    } else {
        var dateSplit = calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)].split('/');
        updobj.value = dateSplit[2] + '-' + parseInt(dateSplit[0],10).toPaddedString(2,10) + '-' + parseInt(dateSplit[1],10).toPaddedString(2,10);
    }
	//updobj.value=calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)];
	getObj('fc').style.display='none';
	
}

function cs_blur(e) {
	//updobj.value=calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)];
	getObj('fc').style.display='none';
	
}

var mn=new Array('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC');
var mnn=new Array('31','28','31','30','31','30','31','31','30','31','30','31');
var mnl=new Array('31','29','31','30','31','30','31','31','30','31','30','31');
var calvalarr=new Array(42);

function f_cps(obj) {
	obj.style.background='#C4D3EA';
	obj.style.font='10px Arial';
	obj.style.color='#333333';
	obj.style.textAlign='center';
	obj.style.textDecoration='none';
	obj.style.border='1px solid #6487AE';
	obj.style.cursor='pointer';
}

function f_cpps(obj) {
	obj.style.background='#C4D3EA';
	obj.style.font='10px Arial';
	obj.style.color='#ABABAB';  // grayed out
	obj.style.textAlign='center';
	obj.style.textDecoration='line-through';
	obj.style.border='1px solid #6487AE';  //grayed out
	obj.style.cursor='default';
}

function f_hds(obj) {
	obj.style.background='#FFF799';
	obj.style.font='bold 10px Arial';
	obj.style.color='#333333';
	obj.style.textAlign='center';
	obj.style.border='1px solid #6487AE';
	obj.style.cursor='pointer';
}

// day selected
function prepcalendar(hd,cm,cy) {
	now=new Date();
	//sd=now.getDate();
	now.setYear(2020);
	now.setMonth(0);
	now.setDate(1);
	sd=now.setFullYear(1980,0,1);
	sd = now;
	td=new Date();
	td.setDate(1);
	td.setFullYear(cy);
	td.setMonth(cm);
	cd=td.getDay();
	getObj('mns').innerHTML=mn[cm]+ ' ' + cy;
	marr=((cy%4)==0)?mnl:mnn;
	for(var d=1;d<=42;d++) {
		f_cps(getObj('v'+parseInt(d)));
		if ((d >= (cd -(-1))) && (d<=cd-(-marr[cm]))) {
			//dip=((d-cd < sd)&&(cm==sccm)&&(cy==sccy));
			dip=false;
			htd=((hd!='')&&(d-cd==hd));
			/*if (dip) {
				//f_cpps(getObj('v'+parseInt(d)));
			}
			else*/ if (htd)
				f_hds(getObj('v'+parseInt(d)));
			else
				f_cps(getObj('v'+parseInt(d)));
            
			getObj('v'+parseInt(d)).onmouseover=(dip)?null:cs_over;
			getObj('v'+parseInt(d)).onmouseout=(dip)?null:cs_out;
			getObj('v'+parseInt(d)).onclick=(dip)?null:cs_click;
			
			getObj('v'+parseInt(d)).innerHTML=d-cd;	

			calvalarr[d]=''+ (cm-(-1)) +'/'+ (d-cd) +'/'+ cy;

		}
		else {
			getObj('v'+d).innerHTML='&nbsp;';
			getObj('v'+parseInt(d)).onmouseover=null;
			getObj('v'+parseInt(d)).onmouseout=null;
			getObj('v'+parseInt(d)).style.cursor='default';
			}
	}
}

prepcalendar('',ccm,ccy);
//getObj('fc'+cc).style.visibility='hidden';

function caddm() {
	marr=((ccy%4)==0)?mnl:mnn;
	
	ccm+=1;
	if (ccm>=12) {
		ccm=0;
		ccy++;
	}
	//cdayf();
	prepcalendar('',ccm,ccy);
}

function csubm() {
	marr=((ccy%4)==0)?mnl:mnn;
	
	ccm-=1;
	if (ccm<0) {
		ccm=11;
		ccy--;
	}
	//cdayf();
	prepcalendar('',ccm,ccy);
}

function cdayf() {
if ((ccy>sccy)|((ccy==sccy)&&(ccm>=sccm)))
	return;
else {
	ccy=sccy;
	ccm=sccm;
	cfd=scfd;
	}
}